﻿namespace Vdc.Grid
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Xml;
    using System.Xml.Schema;

    public class Localization
    {
        private bool design;
        private XmlDocument doc;
        private int errors_number;
        private string errors_string;
        private XmlValidatingReader reader;
        public XmlElement root;
        public string ServerPath;
        private XmlTextReader txtReader;
        private XmlTextReader xmsReader;

        public Localization(string path, string lang, Page page) : this(path, lang, page, false)
        {
        }

        public Localization(string path, string lang, Page page, bool outside)
        {
            this.errors_string = "Grid's Localization loading error:";
            this.ServerPath = "";
            ValidationEventHandler handler = new ValidationEventHandler(this.ValidationCallback);
            try
            {
                if (outside)
                {
                    this.txtReader = new XmlTextReader(path + @"\" + lang + ".xml");
                    this.xmsReader = new XmlTextReader(path + @"\Schema.xml");
                }
                else if (page == null)
                {
                    this.txtReader = new XmlTextReader(HttpContext.Current.Server.MapPath(path + "/" + lang + ".xml"));
                    this.xmsReader = new XmlTextReader(HttpContext.Current.Server.MapPath(path + "/Schema.xml"));
                }
                else
                {
                    this.txtReader = new XmlTextReader(page.MapPath(path + "/" + lang + ".xml"));
                    this.xmsReader = new XmlTextReader(page.MapPath(path + "/Schema.xml"));
                }
                this.reader = new XmlValidatingReader(this.txtReader);
                this.reader.ValidationType = ValidationType.XDR;
                XmlSchemaCollection schema = new XmlSchemaCollection();
                schema.Add("obout:grid:localization-schema", this.xmsReader);
                this.reader.Schemas.Add(schema);
                this.reader.ValidationEventHandler += handler;
                this.doc = new XmlDocument();
                this.doc.Load(this.reader);
            }
            catch (Exception exception)
            {
                if (!this.design)
                {
                    throw new Exception("Grid's Localization parsing error: " + exception.Message);
                }
            }
            finally
            {
                if (this.txtReader != null)
                {
                    this.txtReader.Close();
                }
                if (this.xmsReader != null)
                {
                    this.xmsReader.Close();
                }
                if (this.reader != null)
                {
                    this.reader.Close();
                }
            }
            if (this.errors_number > 0)
            {
                throw new Exception(this.errors_string);
            }
            if (this.doc != null)
            {
                this.root = this.doc.DocumentElement;
            }
        }

        public XmlNodes getCollection(XmlNode parent, string name)
        {
            XmlNodes nodes = new XmlNodes();
            XmlNode node = (parent == null) ? this.root : parent;
            if (node != null)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    XmlNode node2 = node.ChildNodes[i];
                    if ((node2.Name.ToLower() == name.ToLower()) && (node2.NodeType == XmlNodeType.Element))
                    {
                        nodes.Add(node2);
                    }
                }
            }
            return nodes;
        }

        private void ValidationCallback(object sender, ValidationEventArgs args)
        {
            this.errors_string = this.errors_string + "  " + args.Message;
            this.errors_number++;
        }
    }
}

